package com.example.wang.yuejian.Model;

import com.example.wang.yuejian.RetrofitListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by liningning on 2017/10/9.
 */

public class RetrofitBaseModel {
    protected Retrofit retrofit;
    private static final String BASE_URL="http://10.0.2.2/yuejian/public/index.php/index/api/";
    public RetrofitBaseModel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                //增加返回值为Gson的支持(以实体类返回)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public <T> void bindCallback(Call<T> call,final RetrofitListener<T> listener) {
        Callback<T> callback=new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                listener.onFail(t.toString());
            }
        };
       call.enqueue(callback);
    }
}
